﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebUI.Controls;
using POSSystem.BLL;

namespace WebUI.EditPages
{
    public partial class RepairSales : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);
            saleLookup.OnAddIMEI = saleLookup_OnAddIMEI;
            saleLookup.OnAddProduct = saleLookup_OnAddProduct;

            if (!IsPostBack)
            {
                String sessionID = Request.QueryString[KEY];
                repairSale.ClearItems();
                int[] repairIDs = (int[])Session[sessionID];
                for (int i = 0; i < repairIDs.Length; i++)
                {
                    repairSale.AddRepair(repairIDs[i]);
                }

                if (repairIDs.Length > 0)
                {
                    int custID = RepairJob.GetCustID(repairIDs[0]);
                    if (custID > 0)
                        repairSale.DefaultCustID = custID;
                }

                Session.Remove(sessionID);
            }
        }

        public void saleLookup_OnAddIMEI(object sender, ListItemEventArgs e)
        {
            try
            {
                string imei = e.Data["IMEI"].ToString();
                repairSale.AddIMEI(imei);
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("Sales.aspx.cs", "saleLookup_OnAddIMEI", ex.StackTrace);
                ((Popup)Master).ShowMessage("(*) Error in add IMEI: " + ex.Message);
                //updateErr.Update();
            }
        }

        public void saleLookup_OnAddProduct(object sender, ListItemEventArgs e)
        {
            try
            {
                int productID = (int)e.Data["ProductID"];
                int quantity = (int)e.Data["Quantity"];
                repairSale.AddProduct(productID, quantity);
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("Sales.aspx.cs", "saleLookup_OnAddProduct", ex.StackTrace);
                ((Popup)Master).ShowMessage("(*) Error in add product: " + ex.Message);
                //updateErr.Update();
            }
        }
    }
}